<?php require_once 'httpFilter.php'; 'conf.php'; ?>
<?php include("includes/libs.php"); ?>
<html>
<title>View My Accounts</title>
<head>
	<link rel="stylesheet" type="text/css" media="screen" href="css/styles.css" />
	<style type="text/css">
		.totalCaption { font-size: 1.1em; font-weight: bold; }
	</style>

<script src="http://malsup.github.com/jquery.form.js"></script>
<script type="text/javascript" src="myaccounts.js"></script>
<script type="text/javascript" src="js/utils.js"></script>

<!--[if IE]><script language="javascript" type="text/javascript" src="plugins/jqplot/excanvas.min.js"></script><![endif]-->
<script language="javascript" type="text/javascript" src="plugins/jqplot/jquery.jqplot.min.js"></script>
<link rel="stylesheet" type="text/css" href="plugins/jqplot/jquery.jqplot.css" />

<script type="text/javascript" src="plugins/jqplot/jqplot.categoryAxisRenderer.min.js"></script>
<script type="text/javascript" src="plugins/jqplot/jqplot.barRenderer.min.js"></script>
<script type="text/javascript" src="plugins/jqplot/jqplot.dateAxisRenderer.min.js"></script>
<script type="text/javascript" src="plugins/jqplot/jqplot.pointLabels.js"></script>

</head>
<body>
	<div id="msgDlg"></div>
	<div id="confirmDlg"></div>
	<input type="hidden" id="budgetId" value="<?=$_GET["budgetId"]?>"/>
	
	<div id="chartdiv" style="height:250px; width:500px; display:block; position: absolute; right:0px; bottom:20px;">
		<a href="#" onclick="hideGraph();"><img src="images/delete.png" style="width: 1.5em; top: 15px; position:absolute; right: 2px; z-index: 10;" /></a>
	</div>
	<div id="loadingPic" style="display: none; position:absolute; left: 0px; top:0px; height:100%; width: 100%; text-align: center;
	           background-image:url('images/translucent.gif'); z-index: 1000;">
		<img src="images/loading2.gif" style="margin: 12%"/>
	</div>
	
	<?php include("includes/navigation.php"); ?>
	
	
	<table id="mytable" cellspacing="0" summary="August 2010 Budget">
	<caption>"Getting Rich Slowly"</caption>
	<tr>
	    <th scope="row" colspan="6" class="spec">Accounts View</th>
	  </tr>
	  <tr class="incomeHeader">
	    <th scope="col">Account name</th>
	    <th scope="col">Account number</th>
	    <th scope="col">Balance</th>
	    <th scope="col">Stmt Balance</th>
	     <th scope="col">Available</th>
	    <th scope="col">
	    	<a href="#" id="createAccLink"><img src="images/new.png" title="Create new account" alt="Create new account" border="0" width="15"/></a>
		    	&nbsp;&nbsp;
	    	<a href="#" id="deleteAccLink"><img src="images/delete.png" title="Delete selected account" alt="Delete selected account" border="0" width="15"/></a>
	    </th>
	  </tr>
	 <?php

	 		$userid = $_SESSION["userid"];
	
	 		require_once "classes.php";
	 		require_once "services/budgetServices.php";
			$service = new CommonServices;
			$budgetServices = new BudgetService;
				 
			 $con = mysql_connect(GlobalConfig::db_host_name,GlobalConfig::db_username,GlobalConfig::db_password);
			if (!$con)
			  {
			  die('Could not connect: ' . mysql_error());
			  }
			
			mysql_select_db(GlobalConfig::db_name, $con);
			
			$result = mysql_query("select * from account where userid = ". $userid);
			
			while($row = mysql_fetch_array($result))
			  {
			  	
			  	$transctionView = "";
			  	if ($row['type'] == 1){
			  		$transctionView = "index.php?mod=savingsView";
			  	} else {
			  		$transctionView = "index.php?mod=transactionView";
			  	} 
	 ?>
	  <tr id="<?= $row['id']?>" accountType="<?=$row['type'] ?>" class="accountRow">
	    <th scope="row" class="spec"><a href="<?=$transctionView?>&accId=<?= $row['id']?>" class="accountName"><?=$row['name'] ?></a></th>
	    <td class="spec"><a href="index.php?mod=accountDetails&accId=<?= $row['id']?>" class="accountNumber"><?=$row['accNum'] ?></a></td>
	    <td class="spec">R <label class="total"><?= $service->getBalanceForAcccount($row['id']) ?></label></td>
	    <td class="spec">R <label class="stmtTotal"><?= ($row['stmtBalance'] == '0' ? '-' : $row['stmtBalance'])?></label></td>
	    <td class="spec">R <label class="availableBalance"><?= ($row['available'] == '0' ? '-' : $row['available']) ?></label></td>
	    <td><input type="radio" name="selectAccount" value="<?= $row['id'] ?>"></td>
	  </tr>
	 
	  <? } ?>
	  </table>
	  <br/>
	  
	  
	  <?php
	  		
	  	$difference = $service->getSavingsDifference($_SESSION["userid"]);
	  	$differenceMsg = "";
	  	if ($difference < 0){
	  		$difference = $difference * -1;
	  		$differenceMsg = "Funds to transfer from Savings Account";
	  	} else if ($difference > 0) {
	  		$differenceMsg = "Funds to transfer to Savings Account";
	  	}
	  
	 ?>
	 
	 <fieldset style="width: 675px;"><legend>Totals</legend>
		<table cellpadding="0" cellspacing="0" width="100%" >
		 	<tr>
		 		<td><span>Current Budget Available Funds: </span></td><td><label id="budgetFunds" class="totalCaption"></label></td>
		 	</tr>
		 	<tr>
		 		<td><span>Current Budget Remaining Funds: </span></td><td><label id="budgetFundsRemaining" class="totalCaption"></label></td>
		 	</tr>
		 	<tr>
		  		<td><span> Usable Cash :</span></td><td><label id="usableCash" class="totalCaption"></label></td>
		  	</tr>
		  	<tr>
		  		<td><span> Equity: </span></td><td><label id="equity" class="totalCaption"></label></td>
		  	</tr>
			 <?php
			 	if ($difference != 0){
			?>
			<tr>
		  		<td><span> <?=$differenceMsg ?>: </span></td><td><label id="totalDifference"  class="totalCaption"><?=$difference?></label></td>
		  	</tr>	
			<? } ?>
				
			</table>
	 </fieldset>
	 
	 <br/>
	 <div id="newAccDiv">
	 <table cellpadding="0" cellspacing="0" width="500" id="newAcc" style="display: none;">
	 	<tr>
	    	<th scope="col" colspan="2">Create New Account</th>
	    </tr>
	 	<tr>
	 		<th class="specalt">Account name:</th>
	 		<th class="spec"><input class="textInput" type="text" id="accName" style="text-transform: uppercase"/></th>
	 	</tr>
	 	<tr>
	 		<th class="specalt">Account number:</th>
	 		<th class="spec"><input class="textInput" type="text" id="accNum"/></th>
	 	</tr>
	 	<tr id="openingBalRow">
	 		<th class="specalt">Opening Balance:</th>
	 		<th class="spec"><input class="textInput" type="text" id="openingBal"/></th>
	 	</tr>
	 	<tr id="transactionRow">
	 		<th class="specalt">Account Type:</th>
	 		<?
				if (($budgetServices->getSavingsAccountId($userid) == -1) && (($budgetServices->getFirstTransactionalAccountId($userid) != -1))){
	 		?>
	 		<th class="spec">Savings Expense<input type="radio" name="accountType" value="savings">
	 							Transactional<input type="radio" name="accountType" value="normal" checked="checked"></input>
	 		</th>
	 		<? } else {?>
	 		<th class="spec">Savings<input type="radio" name="accountType"  value="savings" disabled>
	 						Transactional<input type="radio" name="accountType" value="normal" checked="checked" disabled>
	 		</th>
	 		<?}?>
	 	</tr>
	 	<tr id="buttonRow"><th colspan="2" align="center" class="specalt">
	 		<button id="createAccountBtn" onclick="createAccount();">Create Account</button>
			<button id="cancelAccountBtn">Cancel</button>
			</th>
	 	</tr>
	 </table>
	 
	 
	 <table cellpadding="0" cellspacing="0" width="500" id="WizPage2" style="display: none;">
	 </table>
	 </div>
	 
	 <div id="wizardDiv" style="display: none;">
	 	<div id="wizCreateNewAccount" style="display: none; font-size: 0.8em;" class="wizStep">	 	
		 	<form id="fileUploadForm" name="fileUploadForm" method="post" enctype="multipart/form-data" action="index.php?mod=uploadStatementFirstTime">
		 		<h1>Budget:</h1>
			 	<p>What date of the month would you like your budget to start?<br/> 
			 		To most people, this would be the day they get paid.</p>
			 	<input id="budgetStartDate" name="budgetStartDate" type="text" class="shortest"/> &nbsp; of every month.
			 	<hr/>
			 	<h1>Account Details:</h1>
			 	<table>
				 	<tr>
			 			<td>Account Name:</td><td> <input type="text" id="accName" name="accName" /></td>
			 		</tr>
			 		<tr>
						<td>Account Type:</td><td><select name="accountTypeSelect" id="accountTypeSelect">
							<option value="" selected="selected">Please Select</option>
						 		<?
						 			$accountTypes = $budgetServices->getAccountTypes();
									foreach ($accountTypes as $accountTypeKey => $accountTypeValue) {
									?>
										<option value="<?=$accountTypeKey?>"><?=$accountTypeValue?></option>
									<?
									}
						 		?>	
				 			</select></td>
		 			</tr>		 			
				 </table>
		 		<hr/>
		 		<h1>Select your statement and upload:</h1>
		 		<table>
				 	<tr>
			 			<td>File Type**:</td>
			 			<td>CSV <input type="radio" value="CSV" name="uploadStatementType"/>
			 				OFX <input type="radio" value="OFX" name="uploadStatementType" /></td>
	 				</tr>
	 				<tr id="OFXAvailableAmountTR">
			 			<td>Available Amount:</td><td><input type="text" name="availableAmnt" id="availableAmnt" ><em>same as statement balance if left empty which might not be correct</em></td>
	 				</tr>
		 			<tr>
						<td>Select File:</td><td><input id="transactionFile" type="file" class="longTextInput" name="transactionFile"/></td>
					</tr>
				</table>
				<input type="hidden" id="action" name="action" value="uploadStatement"/>
			</form>
			<p>**Supported statements: FNB csv or ANY bank's OFX statement file</p>
	 	</div>
	 </div>
	 
	 
	 
	 <?php
		mysql_close($con);
	 ?>
	 
	 <?php include("includes/footer.php"); ?>
	</body>
</html>